package com.shy.springframework.context.annotation;

import com.shy.springframework.beans.factory.config.BeanDefinition;
import com.shy.springframework.stereotype.Component;
import com.shy.springframework.util.ClassUtils;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 包扫描器
 * 扫描指定包名下使用Component注解的类， 生成BeanDefinition集合
 *
 */
public class ClassPathScanningCandidateComponentProvider {

    public Set<BeanDefinition> findCandidateComponents(String basePackage){
        Set<BeanDefinition> candidates = new LinkedHashSet<>();
        Set<Class<?>> classes =  ClassUtils.scanPackageByAnnotation(basePackage, Component.class);
        for (Class<?> clazz : classes) {
            candidates.add(new BeanDefinition(clazz));
        }
        return candidates;
    }
}
